package com.aruba.ioclib

import android.view.View
import java.lang.ref.WeakReference
import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method

/**
 * 动态代理事件
 * Created by aruba on 2021/10/27.
 */
class ClickInvocationHandler(
    private val activity: WeakReference<BaseActivity>,
    private val map: Map<String, Method>
) : InvocationHandler {

    override fun invoke(proxy: Any?, method: Method?, args: Array<out Any>?): Any? {
        //发现Method是onClick方法，执行被OnClick注解的clickView方法
        method?.apply {
            activity.get()?.run {
                //onClick回调函数的入参为view: View?，将它强转成View后再传给clickView方法
                return map[name]?.invoke(this, args!![0] as View)
            }
        }

        return method?.invoke(proxy, args)
    }

}